﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public abstract class OptionBasePanel : BasePanel
{
    protected CanvasGroup cg;//canvasGroup,执行隐藏操作的
    protected bool activated;//是否激活
    protected ButtonStatus buttonSatus;
    public CanvasGroup up, down, left, right;
    protected Vector3 MousePos;//鼠标
    protected Vector3 CenterPos;//中心
    protected Vector3 Excursion;//鼠标偏移
    public Text up1, up2, right1, right2, left1, left2, down1, down2;

    public override void OnEnter()
    {
        if (cg == null)
            cg = gameObject.GetComponent<CanvasGroup>();
        cg.alpha = 1;
        cg.blocksRaycasts = true;
        cg.interactable = true;
        activated = true;
        Tag.playerPause = true;
        buttonSatus = ButtonStatus.NULL;
        ChangeOption(buttonSatus);
        CenterPos = transform.position;
        setCostText();
        Input.ResetInputAxes();
    }

    public override void OnExit()
    {
        cg = gameObject.GetComponent<CanvasGroup>();
        cg.alpha = 0;
        cg.blocksRaycasts = false;
        cg.interactable = false;
        activated = false;
        Tag.playerPause = false;
        StatusBar.Instance.SetTarget(null);
    }

    public override void OnPause()
    {
        cg = gameObject.GetComponent<CanvasGroup>();
        cg.alpha = 0;
        cg.blocksRaycasts = false;
        cg.interactable = false;
        activated = false;
    }

    public override void OnResume()
    {
        cg = gameObject.GetComponent<CanvasGroup>();
        cg.alpha = 1;
        cg.blocksRaycasts = true;
        cg.interactable = true;
        activated = true;
        Tag.playerPause = true;
        buttonSatus = ButtonStatus.NULL;
        ChangeOption(buttonSatus);
        CenterPos = transform.position;
        Input.ResetInputAxes();
    }

    public void ChangeOption(ButtonStatus status)
    {
        switch(status)
        {
            case ButtonStatus.UP:
                UpOptionStatus();
                break;
            case ButtonStatus.DOWN:
                DownOptionStatus();
                break;
            case ButtonStatus.RIGHT:
                RightOptionStatus();
                break;
            case ButtonStatus.LEFT:
                LeftOptionStatus();
                break;
            case ButtonStatus.NULL:
                InnerOptionStatus();
                break;
            default:
                break;
        }
    }

    /// <summary>
    /// 五个按键选中状态
    /// </summary>
    protected virtual void UpOptionStatus()
    {
        up.alpha = 1;
        down.alpha = 0;
        left.alpha = 0;
        right.alpha = 0;
    }

    protected virtual void DownOptionStatus()
    {
        up.alpha = 0;
        down.alpha = 1;
        left.alpha = 0;
        right.alpha = 0;
    }

    protected virtual void RightOptionStatus()
    {
        up.alpha = 0;
        down.alpha = 0;
        left.alpha = 0;
        right.alpha = 1;
    }

    protected virtual void LeftOptionStatus()
    {
        up.alpha = 0;
        down.alpha = 0;
        left.alpha = 1;
        right.alpha = 0;
    }
    protected virtual void InnerOptionStatus()
    {
        up.alpha = 0;
        down.alpha = 0;
        left.alpha = 0;
        right.alpha = 0;
    }

    /// <summary>
    /// 五个按键操作
    /// </summary>
    protected virtual void DoUpOption()
    {

    }

    protected virtual void DoDownOption()
    {

    }

    protected virtual void DoRightOption()
    {

    }

    protected virtual void DoLeftOption()
    {

    }

    protected virtual void DoInnerOption()
    {
        UIManager.Instance.PopPanel();
    }

    protected void Start()
    {
        if (cg == null)
            cg = gameObject.GetComponent<CanvasGroup>();
        CenterPos = transform.position;
    }

    protected virtual void Update()
    {
        if (activated && (Input.GetKeyDown(KeyCode.Escape) || Input.GetMouseButtonDown(1)))
        {
            DoInnerOption(); 
        }
        if (activated)
        {
            if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
            {
                switch (buttonSatus)
                {
                    case ButtonStatus.UP:
                        DoUpOption();
                        break;
                    case ButtonStatus.DOWN:
                        DoDownOption();
                        break;
                    case ButtonStatus.RIGHT:
                        DoRightOption();
                        break;
                    case ButtonStatus.LEFT:
                        DoLeftOption();
                        break;
                    case ButtonStatus.NULL:
                        DoInnerOption();
                        break;
                    default:
                        break;
                }
            }

            if (Tag.mouseDelay > 0.5)
            {
                if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
                {
                    buttonSatus = ButtonStatus.UP;
                }
                if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
                {
                    buttonSatus = ButtonStatus.DOWN;
                }
                if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
                {
                    buttonSatus = ButtonStatus.LEFT;
                }
                if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
                {
                    buttonSatus = ButtonStatus.RIGHT;
                }
            }
            else
            {
                MousePos = Input.mousePosition;
                Excursion = MousePos - CenterPos;
                if (Function.DistanceIgnoreZ(MousePos, CenterPos) < 70)//圈内
                {
                    buttonSatus = ButtonStatus.NULL;
                }
                else
                {
                    if (Excursion.x >= Excursion.y && Excursion.x >= -Excursion.y)//right
                    {
                        buttonSatus = ButtonStatus.RIGHT;
                    }
                    if (Excursion.x >= Excursion.y && Excursion.x < -Excursion.y)//down
                    {
                        buttonSatus = ButtonStatus.DOWN;
                    }
                    if (Excursion.x < Excursion.y && Excursion.x >= -Excursion.y)//up
                    {
                        buttonSatus = ButtonStatus.UP;
                    }
                    if (Excursion.x < Excursion.y && Excursion.x < -Excursion.y)//left
                    {
                        buttonSatus = ButtonStatus.LEFT;
                    }
                }
            }
            ChangeOption(buttonSatus);
        }
    }

    protected bool construction(BuildingType type)
    {
        if (!APController.Instance.CheckDecreaseAPForBuilding(type) || !ResourceManager.Instance.CheckResourceForBuilding(type))
        {
            //UIManager.Instance.PopPanel();
            return false;
        }
        APController.Instance.TryDecreaseAPForBuilding(type);
        ResourceManager.Instance.TryConsumeResourceForBuilding(type);
        MapManager.Instance.UpdateDistIgnoreTower();
        EffectObjectManager.Instance.TowerBuildEffects();
        Vector3 worldPos = Camera.main.ScreenToWorldPoint(CenterPos);
        Vector2Int pos = Function.WorldPosToLogicPos(worldPos.x, worldPos.y);
        EffectObjectManager.Instance.CreateEffectObject("Prefabs/Effect/Smoke_Build", Function.LogicPosToWorldPos(pos));
        MapManager.Instance.PlaceTower(type, pos.x, pos.y);
        return true;
    }

    protected virtual void setCostText()
    {
        up1.text = "";
        up2.text = "";
        down1.text = "";
        down2.text = "";
        right1.text = "" ;
        right2.text = "" ;
        left1.text = "" ;
        left2.text = "" ;
    }
}
